pin=4
gpio.mode(pin,gpio.OUTPUT)
_print=print;_ux=uart.write;DLED=true;HRV=nil;send=nil
function sleep(timex) for i=1,timex,1 do tmr.delay(1000) end end
function ith(a) local b=a%16;local c=(a-b)/16;if(b>9)then b=string.char(b+55);end;if(c>9)then c=string.char(c+55);end;return c..b;end
ID=string.sub(string.upper(encoder.toHex(crypto.hash("sha1",wifi.sta.getmac()..wifi.ap.getmac()))),1,16);
 wifi.setmode(wifi.STATION)wifi.sta.config("CMCC_H","")wifi.sta.connect()
 tmr.alarm(1, 1000, 1, function()
      if wifi.sta.getip() == nil then --print("Connecting...")
	  else tmr.stop(1); --print("Connected, IP is "..wifi.sta.getip())
	  r()end
 end)
 function r()cli=net.createConnection(net.TCP,0);
 cli:on("receive", function(sck, c)
 if(DLED)then gpio.write(pin,gpio.LOW)end
 local h=string.sub(c,1,1);
 c=string.sub(c,2);
 if(h=="\01")then 
		cli:send("\255"..c)
	elseif(h=="\255") then 
		t,e=pcall(node.input,c);
		if(t==false)then cli:send("\04"..e)end
	elseif(h=="\254") then
		t,e=pcall(loadstring(c));
		print(t,e)
	elseif(h=="\253")then
		--特殊包
	elseif(h=="\6")then
		if(HRV~=nil) then HRV(c)end
end
if(DLED)then gpio.write(pin,gpio.HIGH) end
end)
cli:on("connection", function(sck, c)
	print=function(...)
		local arg={...};
		local r="";
		for i,v in ipairs(arg) do 
			r=r.." "..tostring(v) 
		end 
		cli:send("\03"..r)
	end;
	cli:send("\01"..ID.."\01")--01启用虚拟机
	send=function(data)cli:send("\6"..data)end
end)
cli:on("disconnection",function(sck,c)print=_print;r();end)
cli:connect(11211,"os.wlly.ltd")
 end

